<style>
    #container {
        width:100%;
        height:300px;
    }

</style>


<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" close="true">
        <span aria-hidden="true">×</span>
    </button>
    <h4 class="modal-title">
        请选择地点
    </h4>
</div>
<div class="modal-body">

    <input type="hidden" id="lng" value="{$lng}">
    <input type="hidden" id="lat" value="{$lat}">
    <input type="hidden" id="address" value="{$address}">

    <div id="container" class="map" tabindex="0"></div>
    <div id='tip'>
        <input type="text" name="search" id="search" value="" placeholder="输入关键字搜索地址"/>
        <span>取消</span>
    </div>

</div>

<div class="modal-footer">
    <button type="button" class="layui-btn layui-btn-primary js_quit" close="true">取消</button>
    <button type="button" class="layui-btn js_add" _event="newmap" >确定</button>
</div>


<script type="text/javascript">
    $(".js_add").click(function () {
        $(".js_quit").click();
    });
    var lat,lng;
    var lng = $("#lng").val();
    var lat = $("#lat").val();
    AMapUI.loadUI(['misc/PositionPicker','misc/PoiPicker'], function(PositionPicker,PoiPicker) {
        if(lat!='' && lng!=''){
            var map = new AMap.Map('container', {
                zoom:14,
                scrollWheel: false,
                center: [lng,lat],
            });
        }else{
            var map = new AMap.Map('container', {
                zoom:14,
                scrollWheel: false,
            });
        }

        var poiPicker = new PoiPicker({
            input: 'search',
            placeSearchOptions: {
                map: map,
                pageSize: 6 //关联搜索分页
            }

        });

        // 加载定位插件，实现定位功能
        AMap.plugin('AMap.Geolocation', function () {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                maximumAge: 0,           //定位结果缓存0毫秒，默认：0
                showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
                showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
                panToLocation: false,     //定位成功后将定位到的位置作为地图中心点，默认：true
                zoomToAccuracy:true      //定位成功后调整地图视野范围使定位位置及精度范围视野s内可见，默认：false
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();

        });
        // 搜索选址
        poiPicker.on('poiPicked', function(poiResult) {
            poiPicker.hideSearchResults();
            lat=poiResult.item.location.lat
            lng=poiResult.item.location.lng
            map.panTo([lng,lat]);
        });
        function onComplete(data) {
            $('#address').val(data.formattedAddress)
            $('.address .info').text(data.formattedAddress)
        }
        //解析定位错误信息
        function onError(data) {
            console.log("定位失败");
            $('.address .info').text('定位失败！');
        }
        var positionPicker = new PositionPicker({
            mode: 'dragMap', //dragMarker
            map: map
        });

        // 拖拽选址

        positionPicker.on('success', function(positionResult) {
            var lng = positionResult.position.lng;
            var lat = positionResult.position.lat;
            var address = positionResult.address;

            $("#lat").val(lat);
            $("#lng").val(lng);
            $("#address").val(address);
        });
        positionPicker.on('fail', function(positionResult) {
            $('.poi .nearpoi').text('')
            $('.address .info').text('')
        });
        var onModeChange = function(e) {
            positionPicker.setMode(e.target.value)
        }
        positionPicker.start();
        map.panBy(0, 1);
        map.addControl(new AMap.ToolBar({
            liteStyle: true
        }))
    });
    function sub(){
        // 提交地址相关操作
    }








    var windowsArr = [];
    var marker = [];
    /*window.init = function(){
        var map = new AMap.Map('container', {
            // center:[117.000923,36.675807],
            resizeEnable: true,
            zoom:11
        });
        AMap.plugin(['AMap.Autocomplete','AMap.PlaceSearch'],function(){
            var autoOptions = {
                //city: "北京", //城市，默认全国
                input: "keyword"//使用联想输入的input的id
            };
            autocomplete= new AMap.Autocomplete(autoOptions);
            var placeSearch = new AMap.PlaceSearch({
                //city:'北京',
                map:map
            })
            AMap.event.addListener(autocomplete, "select", function(e){
                //TODO 针对选中的poi实现自己的功能
                placeSearch.setCity(e.poi.adcode);
                placeSearch.search(e.poi.name)
            });
        });

        AMap.plugin('AMap.Geolocation', function() {
            var geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：5s
                maximumAge: 0,           //定位结果缓存0毫秒，默认：0
                showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
                showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
                panToLocation: true,     //定位成功后将定位到的位置作为地图中心点，默认：true
                zoomToAccuracy:true      //定位成功后调整地图视野范围使定位位置及精度范围视野s内可见，默认：false

            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition(function(status,result){
                if(status=='complete'){
                    onComplete(result)
                }else{
                    onError(result)
                }
            });
        });

        //解析定位结果
        function onComplete(data) {
            var str = [];
            str.push('定位结果：' + data.position);
            str.push('定位类别：' + data.location_type);
            if(data.accuracy){
                str.push('精度：' + data.accuracy + ' 米');
            }//如为IP精确定位结果则没有精度信息
            str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
        }

        //解析定位错误信息
        function onError(data) {
            document.getElementById('status').innerHTML='定位失败'
            document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message;
        }
    }*/





</script>

